package com.sigmatrix.h5.web.controller.utils;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sigmatrix.h5.entity.v2.DisTerminalShop;
import com.sigmatrix.h5.entity.v2.DisTerminalShopDetail;
import com.sigmatrix.h5.service.db.DisTerminalShopDetailDBservice;
import com.sigmatrix.h5.service.db.DisTerminalShopDBService;
import com.sigmatrix.h5.web.BaseController;

/**
 * 通过DisTerminalShop测试mybatic常用操作
 * 
 * @author wei.wang
 *
 */
@RestController
@RequestMapping("/util/test")
public class TestShopController extends BaseController {

	@Autowired
	private DisTerminalShopDBService disTerminalShopImpl;
	
	@Autowired
	private DisTerminalShopDetailDBservice disTerminalShopDetailImpl;

	//ok
	@GetMapping("/shop/select/id/{id}/esecode/{esecode}")
	public DisTerminalShop selectById(HttpServletRequest request,@PathVariable Integer id,@PathVariable String esecode) {
		System.out.println(request.getRequestURI());
		System.out.println("====================");
		return disTerminalShopImpl.selectById(id, "_"+esecode);
	}
	
	//ok
	@PostMapping("/shop/selectList/esecode/{esecode}")
	public List<DisTerminalShop> selectList(@PathVariable String esecode,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.selectList(new EntityWrapper<DisTerminalShop>(shop));
	}
	//ok
	@GetMapping("/shop/delete/id/{id}/esecode/{esecode}")
	public boolean delete(@PathVariable Long id,@PathVariable String esecode) {
		return disTerminalShopImpl.deleteById(id, "_"+esecode);//id类型一定要实体bean的类型一致
	}
	//ok
	@PostMapping("/shop/update/esecode/{esecode}")
	public boolean update(@PathVariable String esecode,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.updateById(shop);
	}
	//ok
	@PostMapping("/shop/count/esecode/{esecode}")
	public int count(@PathVariable String esecode,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.selectCount(new EntityWrapper<DisTerminalShop>(shop));
	}
	
	@GetMapping("/shop/selectBySQL/esecode/{esecode}")
	public List<DisTerminalShop> selectBySQL(@PathVariable String esecode) {
		return disTerminalShopImpl.selectAll("_"+esecode);
	}
	
	@GetMapping("/shop/selectBySQL/esecode/{esecode}/size/{size}/current/{current}")
	public Page<DisTerminalShop> selectBySQL(@PathVariable String esecode,@PathVariable int size,@PathVariable int current) {
		return disTerminalShopImpl.selectAllByPage(new Page<DisTerminalShop>(current, size),"_"+esecode);
	}
	
	@PostMapping("/shop/updateBySQL/esecode/{esecode}")
	public int updateBySQL(@PathVariable String esecode,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.updateLocationById(shop);
	}
	
	//ok
	@PostMapping("/shop/insert/esecode/{esecode}")
	public boolean insert(@PathVariable String esecode,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.insert(shop);
	}
	
	//ok
	@PostMapping("/shop/selectPage/esecode/{esecode}/size/{size}/current/{current}")
	public Page<DisTerminalShop> selectPage(@PathVariable String esecode,@PathVariable int size,@PathVariable int current,@RequestBody DisTerminalShop shop) {
		shop.setTablePrefix("_"+esecode);
		return disTerminalShopImpl.selectPage(new Page<DisTerminalShop>(current, size), new EntityWrapper<>(shop));
	}
	//ok
	@PostMapping("/shopdetail/selectPage/esecode/{esecode}/shopId/{shopId}")
	public void update(@PathVariable String esecode,@PathVariable Integer shopId) {
		DisTerminalShopDetail detail = new DisTerminalShopDetail();
		detail.setTablePrefix("_"+esecode);
		detail.setAddress("xxxxx");
		detail.setProvince(11);
		detail.setCity(11);
		detail.setCitySubOrdinate(111);
		detail.setTerminalShopId(shopId);
		disTerminalShopDetailImpl.updateLocationByShopId(detail);
	}
}
